<template>
  <div class="adoption-apply">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>领养申请</span>
          <el-button type="primary" @click="handleExport">导出记录</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="申请编号">
          <el-input v-model="searchForm.applyNo" placeholder="请输入申请编号" />
        </el-form-item>
        <el-form-item label="申请人">
          <el-input v-model="searchForm.applicant" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="待审核" value="pending" />
            <el-option label="已通过" value="approved" />
            <el-option label="已拒绝" value="rejected" />
            <el-option label="已完成" value="completed" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 申请列表 -->
      <el-table :data="applyList" border style="width: 100%">
        <el-table-column prop="applyNo" label="申请编号" width="180" />
        <el-table-column label="宠物信息" width="300">
          <template #default="scope">
            <div class="pet-info">
              <el-image
                :src="scope.row.petImage"
                style="width: 50px; height: 50px"
                :preview-src-list="[scope.row.petImage]"
              />
              <div class="info-content">
                <div class="pet-name">{{ scope.row.petName }}</div>
                <div class="pet-type">{{ scope.row.petType }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="申请人信息" width="250">
          <template #default="scope">
            <div>{{ scope.row.applicant }}</div>
            <div>{{ scope.row.phone }}</div>
            <div class="address-text">{{ scope.row.address }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="180" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button 
              v-if="scope.row.status === '待审核'"
              type="primary" 
              size="small" 
              @click="handleApprove(scope.row)"
            >
              审核
            </el-button>
            <el-button 
              v-if="scope.row.status === '已通过'"
              type="success" 
              size="small" 
              @click="handleComplete(scope.row)"
            >
              完成领养
            </el-button>
            <el-button 
              type="info" 
              size="small" 
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 审核对话框 -->
    <el-dialog
      title="申请审核"
      v-model="approveDialogVisible"
      width="500px"
    >
      <el-form :model="approveForm" :rules="approveRules" ref="approveFormRef" label-width="100px">
        <el-form-item label="审核结果" prop="result">
          <el-radio-group v-model="approveForm.result">
            <el-radio label="approve">通过</el-radio>
            <el-radio label="reject">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="remark">
          <el-input 
            type="textarea" 
            v-model="approveForm.remark" 
            rows="3"
            placeholder="请输入审核意见"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="approveDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitApprove">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 完成领养对话框 -->
    <el-dialog
      title="完成领养"
      v-model="completeDialogVisible"
      width="500px"
    >
      <el-form :model="completeForm" :rules="completeRules" ref="completeFormRef" label-width="100px">
        <el-form-item label="领养协议" prop="agreement">
          <el-upload
            class="upload-demo"
            action="#"
            :auto-upload="false"
            :on-change="handleFileChange"
          >
            <template #trigger>
              <el-button type="primary">选择文件</el-button>
            </template>
            <template #tip>
              <div class="el-upload__tip">
                请上传已签署的领养协议，支持jpg/pdf格式
              </div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注">
          <el-input 
            type="textarea" 
            v-model="completeForm.remark" 
            rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="completeDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitComplete">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog
      title="申请详情"
      v-model="detailDialogVisible"
      width="700px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="申请编号">{{ currentApply.applyNo }}</el-descriptions-item>
        <el-descriptions-item label="申请状态">
          <el-tag :type="getStatusType(currentApply.status)">
            {{ currentApply.status }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ currentApply.createTime }}</el-descriptions-item>
        <el-descriptions-item label="处理时间">{{ currentApply.handleTime }}</el-descriptions-item>
      </el-descriptions>

      <el-divider content-position="left">宠物信息</el-divider>
      <div class="pet-detail">
        <el-image
          :src="currentApply.petImage"
          style="width: 100px; height: 100px"
          :preview-src-list="[currentApply.petImage]"
        />
        <div class="detail-content">
          <h3>{{ currentApply.petName }}</h3>
          <p>品种：{{ currentApply.petType }}</p>
          <p>年龄：{{ currentApply.petAge }}岁</p>
          <p>性别：{{ currentApply.petGender }}</p>
        </div>
      </div>

      <el-divider content-position="left">申请人信息</el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="姓名">{{ currentApply.applicant }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ currentApply.phone }}</el-descriptions-item>
        <el-descriptions-item label="居住地址" :span="2">{{ currentApply.address }}</el-descriptions-item>
        <el-descriptions-item label="职业">{{ currentApply.occupation }}</el-descriptions-item>
        <el-descriptions-item label="收入水平">{{ currentApply.income }}</el-descriptions-item>
        <el-descriptions-item label="居住条件" :span="2">{{ currentApply.livingCondition }}</el-descriptions-item>
        <el-descriptions-item label="养宠经验" :span="2">{{ currentApply.experience }}</el-descriptions-item>
      </el-descriptions>

      <el-divider content-position="left">处理记录</el-divider>
      <el-timeline>
        <el-timeline-item
          v-for="(record, index) in currentApply.records"
          :key="index"
          :timestamp="record.time"
          :type="record.type"
        >
          {{ record.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  applyNo: '',
  applicant: '',
  status: '',
  dateRange: []
})

// 申请列表数据
const applyList = ref([
  {
    applyNo: 'LY20240318001',
    petName: 'Lucky',
    petType: '金毛',
    petImage: 'https://placeholder.com/100',
    petAge: 2,
    petGender: '公',
    applicant: '张三',
    phone: '13800138000',
    address: '北京市朝阳区xxx街道',
    occupation: '工程师',
    income: '15k-25k',
    livingCondition: '90平米 两居室',
    experience: '曾养过猫咪3年',
    status: '待审核',
    createTime: '2024-03-18 10:00:00',
    handleTime: '',
    records: [
      {
        time: '2024-03-18 10:00:00',
        type: 'primary',
        content: '提交领养申请'
      }
    ]
  },
  {
    applyNo: 'LY20240318002',
    petName: 'Mimi',
    petType: '布偶猫',
    petImage: 'https://placeholder.com/100',
    petAge: 1,
    petGender: '母',
    applicant: '李四',
    phone: '13900139000',
    address: '北京市海淀区xxx小区',
    occupation: '教师',
    income: '10k-15k',
    livingCondition: '70平米 一居室',
    experience: '第一次养宠物',
    status: '已通过',
    createTime: '2024-03-18 09:00:00',
    handleTime: '2024-03-18 11:00:00',
    records: [
      {
        time: '2024-03-18 11:00:00',
        type: 'success',
        content: '审核通过：条件符合要求'
      },
      {
        time: '2024-03-18 09:00:00',
        type: 'primary',
        content: '提交领养申请'
      }
    ]
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 审核对话框
const approveDialogVisible = ref(false)
const approveFormRef = ref(null)
const approveForm = reactive({
  result: '',
  remark: ''
})

const approveRules = {
  result: [{ required: true, message: '请选择审核结果', trigger: 'change' }],
  remark: [{ required: true, message: '请输入审核意见', trigger: 'blur' }]
}

// 完成领养对话框
const completeDialogVisible = ref(false)
const completeFormRef = ref(null)
const completeForm = reactive({
  agreement: '',
  remark: ''
})

const completeRules = {
  agreement: [{ required: true, message: '请上传领养协议', trigger: 'change' }]
}

// 详情对话框
const detailDialogVisible = ref(false)
const currentApply = reactive({})

// 方法定义
const handleSearch = () => {
  ElMessage.success('搜索功能待实现')
}

const resetSearch = () => {
  searchForm.applyNo = ''
  searchForm.applicant = ''
  searchForm.status = ''
  searchForm.dateRange = []
}

const getStatusType = (status) => {
  const types = {
    '待审核': 'warning',
    '已通过': 'success',
    '已拒绝': 'danger',
    '已完成': 'info'
  }
  return types[status] || 'info'
}

const handleApprove = (row) => {
  approveForm.result = ''
  approveForm.remark = ''
  approveDialogVisible.value = true
}

const handleComplete = (row) => {
  completeForm.agreement = ''
  completeForm.remark = ''
  completeDialogVisible.value = true
}

const handleDetail = (row) => {
  Object.assign(currentApply, row)
  detailDialogVisible.value = true
}

const handleExport = () => {
  ElMessage.success('导出功能待实现')
}

const handleFileChange = (file) => {
  completeForm.agreement = file.raw
}

const submitApprove = async () => {
  if (!approveFormRef.value) return
  
  await approveFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('审核处理成功')
      approveDialogVisible.value = false
    }
  })
}

const submitComplete = async () => {
  if (!completeFormRef.value) return
  
  await completeFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('领养完成')
      completeDialogVisible.value = false
    }
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.detail-content {
  margin-bottom: 20px;
}

.detail-content h3 {
  margin: 0 0 10px 0;
}

.detail-content p {
  margin: 5px 0;
  color: #666;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 